package volkov.ja;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import volkov.ja.pages.TestBase;
import volkov.ja.util.TestLogger;

public class MovieRemoveTest1 extends TestBase {
	private static TestLogger LOG;

	@BeforeClass
	public void prepareTest() {
		LOG = new TestLogger(MovieRemoveTest1.class, getSessionId());
		LOG.info("Prepare environment: loading temporary movies to the database");
		db.prepareFixture("tmpMovies");
		loginTestApplication();
	}

	@Test
	public void testMovieRemove1() throws Exception {
		String title = "Not really interesting one";
		LOG.info("Go to the movies list");
		driver.get(baseUrl + "#!/sort/name%20asc/");
		driver.findElement(By.id("q")).sendKeys("" + Keys.RETURN);
		LOG.info("Go to the movie \"" + title + "\"");
		WebElement we = checkElementHavingChildWithContent(
				By.className("movie_box"), By.className("title"), title,
				"Couldn't find the movie \"" + title
						+ "\" in the list for removal");
		String id = we.getAttribute("id");
		we.click();
		LOG.info("Remove the movie \"" + title + "\"");
		clickIfPresent(By.cssSelector("img[alt=\"Remove\"]"));
		handleAlertIfPresent(true,
				"No confirmation dialog appeared on the movie deletion");
		// check results
		driver.get(baseUrl + "#!/sort/name%20asc/");
		checkElementIsAbsent(By.id(id), "Error: Movie \"" + title
				+ "\" is still present after removal");
	}

}
